package org.jinxing.utils;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
/**
 * @FileName：CookieUtil.java
 * @Description：cookie存取 工具类
 * @author
 * @version 1.0
 */
public class CookieUtil {
	private static final Logger log = Logger.getLogger(CookieUtil.class);
	/**
	 * 根据key获取cookie
	 * @param request
	 * @param response
	 * @param key
	 * @return
	 */
	public static Cookie getCookie(HttpServletRequest request,
			HttpServletResponse response, String key) {
		try {
			Cookie[] cookies = request.getCookies();
			if (cookies != null) {
				for (Cookie cookie : cookies) {
					if (cookie.getName().equals(key)) {
						return cookie;
					}
				}
			}
			try {
				Cookie keyInRequest = (Cookie) request.getAttribute(key);
				return keyInRequest;
			} catch (Exception e) {
			}
			return null;
		} catch (Exception e) {
			log.error("[getCookie() ---- Exception return " + e.getMessage()
					+ "]");
			return null;
		}
	}
	/**
	 * 设置cookie
	 * @param request
	 * @param response
	 * @param key
	 * @param value
	 * @return true设置成功， false设置失败
	 */
	public synchronized static boolean setCookie(HttpServletRequest request,
			HttpServletResponse response, String key, String value) {
		try {
			Cookie cookie = new Cookie(key, value);
			cookie.setPath("/");
			cookie.setMaxAge(-1);// 不设置的话，则cookies不写入硬盘,而是写在内存,只在当前页面有用,以秒为单位
			response.addCookie(cookie);
			request.setAttribute(key, cookie);
			return true;
		} catch (Exception e) {
			log.error("[setCookie() ---- Exception return " + e.getMessage()
					+ "]");
			return false;
		}
	}
	/**
	 * 设置cookie
	 * @param request
	 * @param response
	 * @param key
	 * @param value
	 * @param maxAge 设置cookie时间，以秒为单位
	 * @return
	 */
	public synchronized static boolean setCookie(HttpServletRequest request,
			HttpServletResponse response, String key, String value,int maxAge) {
		try {
			Cookie cookie = new Cookie(key, value);
			cookie.setPath("/");
			cookie.setMaxAge(maxAge);
			response.addCookie(cookie);
			request.setAttribute(key, cookie);
			return true;
		} catch (Exception e) {
			log.error("[setCookie() ---- Exception return " + e.getMessage()
					+ "]");
			return false;
		}
	}
	/**
	 * 删除指定的cookie
	 * @param request
	 * @param response
	 * @param key
	 */
	public static void removeCookie(HttpServletRequest request,
			HttpServletResponse response, String key) {
		try {
			Cookie cookie = getCookie(request, response, key);
			if (cookie != null) {
				cookie.setMaxAge(0);
				cookie.setPath("/");
				cookie.setValue(null);
				response.addCookie(cookie);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 得到客户端IP
	 * 
	 * @return String
	 */
	public static String getIpAddr(HttpServletRequest request,
			HttpServletResponse response) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		if (ip != null && ip.indexOf(",") != -1) {
			ip = ip.substring(0, ip.indexOf(","));
		}
		log.info("[getIpAddr() ---- 订购次数限制作为key值ip为---]" + ip);
		return ip;
	}

}
